<?php


namespace app\api\controller;

use app\common\library\common;
use app\common\library\wxpay\FzweixinPay;
use think\facade\Db;
use think\Request;
use think\Response;
use app\common\library\wxpay\WeixinPay;

class Wecat extends Wap
{

    /**
     * 微信支付
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            if (!isset($post['order_on'])) {
                $arr['code'] = 1001;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            switch ($post['type']) {
                case 1:
                    $order = Db::name('customer_recharge_order')->where('order_on', $post['order_on'])->where('mark', 1)->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    break;
                case 2:
                    $order = Db::name('customer_level_order')->where('order_on', $post['order_on'])->where('mark', 1)->find();

                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    break;
                case 3:
                    $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    $order['price'] = $order['pay_price'];
                    break;
                case 4:
                    $order = Db::name('customer_seat_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    break;
                case 5:
                    $order = Db::name('customer_integral_product_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_status'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }

                    $order['price'] = $order['pay_money'];
                    break;
                case 7:
                    $order = Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    $order['price'] = $order['pay_price'];
                    break;
                case 8:
                    $order = Db::name('customer_coupon_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    break;
                case 9:
                    $order = Db::name('customer_cardcoupon_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    //   $order['price'] = $order['price'];
                    break;
            }

            $user = Db::name('member')->where('id', $order['user_id'])->find();
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            if ($wxapp) {
                if ($wxapp['app_id'] && $wxapp['key']) {
                    $appId = $wxapp['app_id'];
                    $appsecret = $wxapp['key'];
                    $MCHID = $wxapp['wc_hid'];

                }
            }
            if (!$user['openid']) {
                $code = $post['code'];
                if (!$code) {
                    $arr['code'] = 1001;
                    $arr['message'] = '参数错误';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                $params = array();
                $params['appid'] = $appId;
                $params['secret'] = $appsecret;
                $params['code'] = $code;
                $params['grant_type'] = 'authorization_code';

                $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appId . '&secret=' . $appsecret . '&js_code=' . $code . '&grant_type=authorization_code';
                $method = "POST";
                $codeUset = $this->curl($url, $method, $params);

                if (!isset($codeUset['openid'])) {
                    $arr['code'] = 1001;
                    $arr['message'] = '获取openid失败';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                $upda['openid'] = $codeUset['openid'];
                Db::name('mini_member_list')->where('id', $user['id'])->update($upda);
                $openId = $codeUset['openid'];
            } else {
                $openId = $user['openid'];
            }
            $total_fee = $order['price'] * 100;
            $body = '订单支付';


            $weixinpay = new WeixinPay($appId, $openId, $MCHID, $wxapp['api_key'], $post['order_on'], $body, $total_fee, $post['type']);
            $return = $weixinpay->pay();
            $arr['code'] = 200;
            $arr['message'] = '成功';
            $arr['data'] = $return;
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 微信支付
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function indexv1(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            if (!isset($post['order_on'])) {
                $arr['code'] = 1001;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            switch ($post['type']) {
                case 1:
                    $order = Db::name('customer_recharge_order')->where('order_on', $post['order_on'])->where('mark', 1)->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    break;
                case 2:
                    $order = Db::name('customer_level_order')->where('order_on', $post['order_on'])->where('mark', 1)->find();

                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    break;
                case 3:
                    $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        Db::name('customer_order')->where('order_on', $post['order_on'])->update(['if_fz' => 1]);
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    $order['price'] = $order['pay_price'];
                    break;
                case 4:
                    $order = Db::name('customer_seat_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_type'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }
                    break;
                case 5:
                    $order = Db::name('customer_integral_product_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        if ($order['pay_status'] > 0) {
                            $arr['code'] = 1001;
                            $arr['message'] = '订单已支付';
                            Response::create($arr, 'json')->send();
                            exit();
                        }
                    }

                    $order['price'] = $order['pay_money'];
                    break;
                case 7:
                    $order = Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->find();
                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    $order['price'] = $order['pay_price'];
                    break;

                case 8:
                    $order = Db::name('customer_coupon_order')->where('order_on', $post['order_on'])->find();

                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    $order['price'] = $order['price'];
                    break;
                case 9:
                    $order = Db::name('customer_cardcoupon_order')->where('order_on', $post['order_on'])->find();

                    if (!$order) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单不存在';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    $order['price'] = $order['price'];
                    break;
            }

            $user = Db::name('member')->where('id', $order['user_id'])->find();
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            if ($wxapp) {
                if ($wxapp['app_id'] && $wxapp['key']) {
                    $appId = $wxapp['app_id'];
                    $appsecret = $wxapp['key'];
                    $mch_id = $wxapp['mch_id'];
                    $api_key = $wxapp['api_key'];

                }
            }
            if (!$user['openid']) {
                $code = $post['code'];
                if (!$code) {
                    $arr['code'] = 1001;
                    $arr['message'] = '参数错误';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                $params = array();
                $params['appid'] = $appId;
                $params['secret'] = $appsecret;
                $params['code'] = $code;
                $params['grant_type'] = 'authorization_code';

                $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appId . '&secret=' . $appsecret . '&js_code=' . $code . '&grant_type=authorization_code';
                $method = "POST";
                $codeUset = $this->curl($url, $method, $params);

                if (!isset($codeUset['openid'])) {
                    $arr['code'] = 1001;
                    $arr['message'] = '获取openid失败';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                $upda['openid'] = $codeUset['openid'];
                Db::name('mini_member_list')->where('id', $user['id'])->update($upda);
                $openId = $codeUset['openid'];
            } else {
                $openId = $user['openid'];
            }
            $total_fee = $order['price'] * 100;
            $body = '订单支付';
            $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();

            $weixinpay = new FzweixinPay($appId, $openId, $mch_id, $api_key, $post['order_on'], $body, $total_fee, $post['type'], $room['sub_mch_id']);
            $return = $weixinpay->pay();
            $arr['code'] = 200;
            $arr['message'] = '成功';
            $arr['data'] = $return;
            Response::create($arr, 'json')->send();
            exit();
        }
    }

    //预约包厢分账

    public function fzbx()
    {
        $list = Db::name('customer_order')->where('pay_type', '>', 0)->where('status', '=', 4)->where('if_fz', '=', 1)->where('transaction_id','>',0)->limit(10)->select();
        foreach ($list as $i => $v) {
            $count = "预约包厢";
            if ($v['pay_type'] == 1) {
                $this->distribution($v['store_id'], round($v['pay_price'] - $v['jia_price'], 2), 1, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            }
            if (($v['pay_type'] == 3 || $v['pay_type'] == 4) && $v['pay_price'] > 0) {
                $count = "抖音美团预约包厢";
                $this->distribution($v['store_id'], round($v['pay_price'] - $v['jia_price'], 2), 1, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            }
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            if ($return == 'SUCCESS') {
                Db::name('customer_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }

    public function fzbxcs(Request $request)
    {
        $post = $request->param();
        if (!isset($post['order_on'])) {
            $arr['code'] = 1001;
            $arr['message'] = '参数错误';
            Response::create($arr, 'json')->send();
            exit();
        }

        $list = Db::name('customer_order')->where('order_on', $post['order_on'])->where('status', '=', 4)->select();
        foreach ($list as $i => $v) {
            $count = "预约包厢";
            if ($v['pay_type'] == 1) {
                $this->distribution($v['store_id'], round($v['pay_price'] - $v['jia_price'], 2), 1, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            }
            if (($v['pay_type'] == 3 || $v['pay_type'] == 4) && $v['pay_price'] > 0) {
                $count = "抖音美团预约包厢";
                $this->distribution($v['store_id'], round($v['pay_price'] - $v['jia_price'], 2), 1, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            }
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            $return = ($return == 'SUCCESS' ? '成功' : '失败');
            if ($return == 'SUCCESS') {
                Db::name('customer_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }

    //加时
    public function fzjs()
    {
        $list = Db::name('customer_renewal_order')->where('status', '=', 4)->where('if_fz', '=', 1)->where('transaction_id','>',0)->limit(10)->select();

        foreach ($list as $i => $v) {
            $count = "包厢加时";
            if ($v['pay_type'] == 1) {
                $this->distribution($v['store_id'], $v['pay_price'], 2, $count, $v['order_id'], $v['order_on'], $v['transaction_id']);
            }
            if (($v['pay_type'] == 3 || $v['pay_type'] == 4) && $v['pay_price'] > 0) {
                $count = "抖音美团包厢加时";
                $this->distribution($v['store_id'], $v['pay_price'], 2, $count, $v['order_id'], $v['order_on'], $v['transaction_id']);
            }
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            if ($return == 'SUCCESS') {
                Db::name('customer_renewal_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }

    public function fzjscs(Request $request)
    {
        $post = $request->param();
        if (!isset($post['order_on'])) {
            $arr['code'] = 1001;
            $arr['message'] = '参数错误';
            Response::create($arr, 'json')->send();
            exit();
        }
        $list = Db::name('customer_renewal_order')->where('order_on', '=', $post['order_on'])->select();

        foreach ($list as $i => $v) {
            $count = "包厢加时";
            if ($v['pay_type'] == 1) {
                $this->distribution($v['store_id'], $v['pay_price'], 2, $count, $v['order_id'], $v['order_on'], $v['transaction_id']);
            }
            if (($v['pay_type'] == 3 || $v['pay_type'] == 4) && $v['pay_price'] > 0) {
                $count = "抖音美团包厢加时";
                $this->distribution($v['store_id'], $v['pay_price'], 2, $count, $v['order_id'], $v['order_on'], $v['transaction_id']);
            }
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            $return = ($return == 'SUCCESS' ? '成功' : '失败');
            if ($return == 'SUCCESS') {
                Db::name('customer_renewal_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';

        }
        return $return;
    }

    //cz
    public function fzcz()
    {
        $list = Db::name('customer_recharge_order')->where('status', '=', 1)->where('if_fz', '=', 1)->select();
        foreach ($list as $i => $v) {
            $count = "充值金额";
            $this->distribution($v['store_id'], $v['price'], 4, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);

            if ($return == 'SUCCESS') {
                Db::name('customer_recharge_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'].'---'.$return.'<p>';
        }
        return $return;
    }

    //cz
    public function fzcoupon()
    {
        $list = Db::name('customer_coupon_order')->where('status', '=', 1)->where('if_fz', '=', 1)->limit(10)->select();
        foreach ($list as $i => $v) {
            $count = "优惠价金额";
            $this->distribution($v['store_id'], $v['price'], 8, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            if ($return == 'SUCCESS') {
                Db::name('customer_coupon_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }

    //cz
    public function fzczcs(Request $request)
    {
        //202305141922078398823021
        $post = $request->param();
        if (!isset($post['order_on'])) {
            $arr['code'] = 1001;
            $arr['message'] = '参数错误';
            Response::create($arr, 'json')->send();
            exit();
        }
        $list = Db::name('customer_recharge_order')->where('order_on', '=', $post['order_on'])->where('status', '=', 1)->where('if_fz', '=', 1)->select();
        foreach ($list as $i => $v) {
            $count = "充值金额";
            $this->distribution($v['store_id'], $v['price'], 4, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            $return = ($return == 'SUCCESS' ? '成功' : '失败');
            if ($return == 'SUCCESS') {
                Db::name('customer_recharge_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }

    public function fzcard()
    {
        $list = Db::name('customer_cardcoupon_order')->where('status', '=', 1)->where('if_fz', '=', 1)->limit(10)->select();
        foreach ($list as $i => $v) {
            $count = "优惠价金额";
            $this->distribution($v['store_id'], $v['price'], 9, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            if ($return == 'SUCCESS') {
                Db::name('customer_cardcoupon_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }
    //cz
    public function fzcpcs(Request $request)
    {
        //202305141922078398823021
        $post = $request->param();
        if (!isset($post['order_on'])) {
            $arr['code'] = 1001;
            $arr['message'] = '参数错误';
            Response::create($arr, 'json')->send();
            exit();
        }
        $list = Db::name('customer_coupon_order')->where('order_on', '=', $post['order_on'])->where('status', '=', 1)->where('if_fz', '=', 1)->select();
        foreach ($list as $i => $v) {
            $count = "团购金额";
            $this->distribution($v['store_id'], $v['price'], 8, $count, $v['id'], $v['order_on'], $v['transaction_id']);
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            $return = ($return == 'SUCCESS' ? '成功' : '失败');
            if ($return == 'SUCCESS') {
                Db::name('customer_coupon_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            }
            echo $v['order_on'] . '---' . $return . '<p>';
        }
        return $return;
    }

    //分账包厢完结
    public function fzwj()
    {
        $list = Db::name('customer_order')->where('store_id', '=', 4)->where('status', '=', 4)->where('if_fz', '=', 1)->select();
        foreach ($list as $i => $v) {
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);


            $endfz = $weixinpay->endfz($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            if ($endfz == 'SUCCESS') {
                Db::name('customer_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 2]);
            } else {
                Db::name('customer_order')->where('order_on', $v['order_on'])->where('if_fz', 1)->update(['if_fz' => 0]);

            }
            echo $v['order_on'] . '---' . $endfz . '<p>';
        }
        return $endfz;
    }

    //分账加时完结
    public function fzjswj()
    {
        $list = Db::name('customer_renewal_order')->where('status', '=', 4)->where('if_fz', '=', 1)->select();
        foreach ($list as $i => $v) {
            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);


            $endfz = $weixinpay->endfz($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
            echo $v['order_on'] . '---' . $endfz . '<p>';
        }
        return $endfz;
    }

    public function fzjg()
    {
        $list = Db::name('customer_order')->where('status', '=', 4)->where('order_on', '202305260338399745144713')->select();
        foreach ($list as $i => $v) {

            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = '', $type = '', $room['sub_mch_id']);
            $return = $weixinpay->fzjg($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['transaction_id'], $v['order_on']);
        }
        return $return;
    }

    public function fx()
    {
        $v = Db::name('customer_order')->where('order_on', '202304261149087809001825')->find();
        $count = '预约包厢';
        return $this->distribution($v['store_id'], $v['price'], 1, $count, $v['id'], $v['order_on'], $v['transaction_id']);

    }

    public function tuikuan(Request $request)
    {
        $post = $request->param();
        if (!isset($post['order_on'])) {
            $arr['code'] = 1001;
            $arr['message'] = '参数错误';
            Response::create($arr, 'json')->send();
            exit();
        }

        $list = Db::name('customer_order')->where('if_fz', '=', 1)->where('order_on', $post['order_on'])->select();
        foreach ($list as $i => $v) {

            $wxapp = Db::name('mini_match')->where('id', 1)->find();
            $room = Db::name('mini_room_list')->where('id', $v['store_id'])->find();
            $weixinpay = new FzweixinPay($wxapp['app_id'], '', $wxapp['mch_id'], $wxapp['api_key'], $v['order_on'], $body = '', $total_fee = $v['price'] * 100, $type = '', $room['sub_mch_id']);
            $return = $weixinpay->refund($wxapp['mch_id'], $room['sub_mch_id'], $wxapp['app_id'], $wxapp['app_id'], $v['order_on']);
        }
        return $return;
    }

    public function yue(Request $request)
    {


//       $order= Db::query("SELECT `status`  FROM `evt_customer_user_balance`   GROUP BY `status` HAVING COUNT( `status`)  > 2");
//        $order = array_column($order, 'status');
//        //var_dump($order);exit();
//        $orderlist= Db::name('customer_user_balance')->whereIn('status',$order)->order('status')->select();
//
//
//        $orders=implode(',',$order);


        $memberlist = Db::name('member')->where('balance', '>', 0)->select();
        foreach ($memberlist as $i => $v) {
            $k = 0;
            $co = 0;
            $cro = 0;
            $cros = 0;
            $sum = 0;
            $osum = 0;
            $csum = 0;
            //订单消耗
            $co = Db::name('customer_order')->where('user_id', $v['id'])->where('balance', '>', 0)->whereIn('status', ['1', '2', '4'])->sum('balance');
            $cro = Db::name('customer_renewal_order')->where('user_id', $v['id'])->where('pay_type', 2)->whereIn('status', ['1', '2', '4'])->sum('pay_price');
//充值的钱
            $cros = Db::name('customer_recharge_order')->alias('cro')
                ->join('customer_user_balance cub', 'cub.status=cro.order_on')
                ->where('cro.status', 1)
                ->where('cro.user_id', $v['id'])
                ->sum('cub.price');
//            if($cros==0 and $v['balance']!=0 ){
//                Db::name('member')->where('id', '=', $v['id'])->update(['balance'=>0]);
//            }
            $osum = round($co + $cro, 2);
            $sum = round($cros - $co - $cro, 2);
            $csum = round($v['balance'] - $sum, 2);
            if ($csum != 0) {
                $k++;
                echo "<p>会员id:" . $v['id'] . ",余额：" . $v['balance'] . ",实际余额" . $sum . ",实际充值总余额:" . $cros . ",实际消耗" . $osum . ",相差" . $csum;

            }

        }
        echo $k;

    }


}